using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMLookupListBusinessObject objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMLookupListBusinessObject.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDMLookupListBusinessObject
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringNotEmpty(dmObjectLookupListBusinessObject.DMClass.ClassNameBO, "dmObjectLookupListBusinessObject.LookupClassName");
            //---------------Execute Test ----------------------
            string actualToString = dmObjectLookupListBusinessObject.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmObjectLookupListBusinessObject.DMClass.ClassNameBO, actualToString);
        }
        [Test]
        public void Test_ToString_LookupClassNameIsNull()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            dmObjectLookupListBusinessObject.DMClass.ClassNameBO = "";
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmObjectLookupListBusinessObject.DMClass.ClassNameBO, "dmObjectLookupListBusinessObject.LookupClassName");
            //---------------Execute Test ----------------------
            string actualToString = dmObjectLookupListBusinessObject.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmObjectLookupListBusinessObject.ToString(), actualToString);
        }
        [Test]
        public void Test_ToString_LookupListIsNull_IDIsNull()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            dmObjectLookupListBusinessObject.DMClass = null;
            dmObjectLookupListBusinessObject.BusinessObjectLookupListID = null;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string actualToString = dmObjectLookupListBusinessObject.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual("-unnamed BOLookupList-", actualToString);
        }

        [Test]
        public void Test_StatusHasWarnings_WhenHasSortOrderProps_ShouldBeFalse()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            DMClass dmClass = dmObjectLookupListBusinessObject.DMClass;
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = property.PropertyName;
            dmObjectLookupListBusinessObject.DMSortOrder = order;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, order.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors;
            bool hasWarnings = dmObjectLookupListBusinessObject.Status.HasWarnings(out errors);
            //---------------Test Result -----------------------
            Assert.IsFalse(hasWarnings);
        }
        [Test]
        public void Test_StatusHasWarnings_WhenNotHasSortOrderProps_ShouldBeTrue()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = "UnknownProp";
            dmObjectLookupListBusinessObject.DMSortOrder = order;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, order.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors;
            bool hasWarnings = dmObjectLookupListBusinessObject.Status.HasWarnings(out errors);
            //---------------Test Result -----------------------
            Assert.IsTrue(hasWarnings);
            Assert.AreEqual(1, errors.Count);
        }

        [Test]
        public void Test_Save_ShouldDeleteDMSortOrder_WhenHasNoSortProperties()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            DMSortOrder dmSortOrder = new DMSortOrder();
            dmObjectLookupListBusinessObject.DMSortOrder = dmSortOrder;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmObjectLookupListBusinessObject.DMSortOrder);
            Assert.AreEqual(0, dmObjectLookupListBusinessObject.DMSortOrder.DMSortProperties.Count);
            Assert.IsTrue(dmSortOrder.Status.IsNew);
            Assert.IsFalse(dmSortOrder.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmObjectLookupListBusinessObject.Save();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmSortOrder.Status.IsNew);
            Assert.IsTrue(dmSortOrder.Status.IsDeleted);
            Assert.IsTrue(dmSortOrder.Status.IsDirty);
            Assert.IsNull(dmObjectLookupListBusinessObject.DMSortOrder);
        }

        [Test]
        public void Test_Save_ShouldNotDeleteDMSortOrder_WhenHasSortProperties()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            DMSortOrder dmSortOrder = new DMSortOrder();
            TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty(dmSortOrder);
            dmObjectLookupListBusinessObject.DMSortOrder = dmSortOrder;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmObjectLookupListBusinessObject.DMSortOrder);
            Assert.AreEqual(1, dmObjectLookupListBusinessObject.DMSortOrder.DMSortProperties.Count);
            Assert.IsTrue(dmSortOrder.Status.IsNew);
            Assert.IsFalse(dmSortOrder.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmObjectLookupListBusinessObject.Save();
            //---------------Test Result -----------------------
            Assert.IsFalse(dmSortOrder.Status.IsNew);
            Assert.IsFalse(dmSortOrder.Status.IsDeleted);
            Assert.IsFalse(dmSortOrder.Status.IsDirty);
            Assert.AreSame(dmSortOrder, dmObjectLookupListBusinessObject.DMSortOrder);
        }

        [Test]
        public void Test_DMSortOrderPropMustBeOnClassRule_ShouldBeOnBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ReadOnlyCollection<IBusinessObjectRule> rules = BOHelper.GetBusinessObjectRules(dmObjectLookupListBusinessObject);
            //---------------Test Result -----------------------
            TestUtilsShared.AssertHasRuleOfType<DMSortOrderPropMustBeOnRelatedClassRule>(rules);
        }

        [Test]
        public void Test_DMSortOrderShouldBeCleared_WhenSelectingNewDMClass()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            dmObjectLookupListBusinessObject.DMClass.Properties.Add(new DMProperty());
            dmObjectLookupListBusinessObject.DMSortOrder = new DMSortOrder();
            DMSortProperty dmSortProperty = new DMSortProperty();
            dmSortProperty.PropertyName = dmObjectLookupListBusinessObject.DMClass.Properties[0].PropertyName;
            dmObjectLookupListBusinessObject.DMSortOrder.DMSortProperties.Add(dmSortProperty);
            //---------------Assert Precondition----------------
            DMSortOrder oldDMSortOrder = dmObjectLookupListBusinessObject.DMSortOrder;
            Assert.IsNotNull(oldDMSortOrder);
            Assert.AreEqual(1, dmObjectLookupListBusinessObject.DMSortOrder.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            dmObjectLookupListBusinessObject.DMClass = new DMClass();
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmObjectLookupListBusinessObject.DMSortOrder);
            Assert.AreNotSame(dmObjectLookupListBusinessObject.DMSortOrder, oldDMSortOrder);
            Assert.AreEqual(0, dmObjectLookupListBusinessObject.DMSortOrder.DMSortProperties.Count);
        }

        [Test]
        public void Test_OwnerClassName_WhenDMClassNull_ShouldReturnEmptyString()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            dmObjectLookupListBusinessObject.DMClass = null;         
            //---------------Assert Precondition----------------
            Assert.IsNull(dmObjectLookupListBusinessObject.DMClass);
            //---------------Execute Test ----------------------
            var ownerClassName = dmObjectLookupListBusinessObject.OwnerClassName;
            //---------------Test Result -----------------------
            Assert.IsNullOrEmpty(ownerClassName);
        }
        [Test]
        public void Test_OwnerClassName_WhenDMClassNotNull_ShouldReturnClassNameBO()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmObjectLookupListBusinessObject = TestUtilsDMBusinessObjectLookupList.CreateUnsavedValidDMBusinessObjectLookupList();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmObjectLookupListBusinessObject.DMClass);
            //---------------Execute Test ----------------------
            var ownerClassName = dmObjectLookupListBusinessObject.OwnerClassName;
            //---------------Test Result -----------------------
            Assert.AreEqual(dmObjectLookupListBusinessObject.DMClass.ClassNameBO ,ownerClassName);
        }
    }
}